/**
 * @作者 Luke
 * @微信公号 欧诺的小书屋
 * @开源项目 $ http://7yue.pro
 * @免费专栏 $ http://course.7yue.pro
 * @我的课程 $ http://imooc.com/t/4294850
 * @创建时间 2020-6-21 21:57
 */
package io.github.talelin.latticy.controller.v1;

import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.dto.course.CreateOrUpdateChapterDTO;
import io.github.talelin.latticy.dto.course.CreateOrUpdateChapterItemDTO;
import io.github.talelin.latticy.dto.course.CreateOrUpdateCourseDTO;
import io.github.talelin.latticy.dto.course.CreateOrUpdateCoursewareDTO;
import io.github.talelin.latticy.model.ChapterDO;
import io.github.talelin.latticy.model.ChapterDetailDO;
import io.github.talelin.latticy.model.CourseDO;
import io.github.talelin.latticy.service.ChapterService;
import io.github.talelin.latticy.service.CourseService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.List;

@RestController
    @RequestMapping("/v1/course")
@Validated
public class CourseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private ChapterService chapterService;

    // 根据教师id获取全部课程
    @GetMapping("/{tid}")
    public List<CourseDO> getCourses(@PathVariable(value = "tid") @Positive Long tid){
        return this.courseService.getCoursesByTeacherId(tid);
    }

    // 根据课程id和教师id获取当前课程的全部章节
    @GetMapping("/chapter/{cid}")
    public List<ChapterDetailDO> getChapters(@PathVariable(value = "cid") @Positive Long cid){
        return this.chapterService.findChapters(cid);
    }

    @GetMapping("search/{title}")
    public List<CourseDO> getCourseByTitle(@PathVariable(value = "title") String title){
        return this.courseService.getCourseByTitle(title);
    }

    // 新增课程
    @PostMapping("create")
    public CreatedVO createCourse(@RequestBody @Validated CreateOrUpdateCourseDTO validator){
        this.courseService.createCourse(validator);
        return new CreatedVO();
    }

    // 增加大章
    @PostMapping("createChapter")
    public CreatedVO createChapter(@RequestBody @Validated CreateOrUpdateChapterDTO validator){
        this.chapterService.createChapter(validator);
        return new CreatedVO();
    }

    // 修改大章
    @PutMapping("update/chapter/{id}")
    public UpdatedVO updateChapter(@PathVariable(name = "id") @Positive Long id,
                                   @RequestBody @Validated CreateOrUpdateChapterDTO validator){
        ChapterDO chapterDO = this.chapterService.getById(id);
        if (chapterDO == null) {
            throw new NotFoundException("chapter not found");
        }
        this.chapterService.updateChapter(chapterDO, validator);
        return new UpdatedVO();
    }

    // 增加小结
    @PostMapping("createChapterItem")
    public CreatedVO createChapterItem(@RequestBody @Validated CreateOrUpdateChapterItemDTO validator){
        this.chapterService.createChapterItem(validator);
        return new CreatedVO();
    }

    // 修改小节
    @PutMapping("update/chapterItem/{id}")
    public UpdatedVO updateChapterItem(@PathVariable(name = "id") @Positive Long id,
                                       @RequestBody @Validated CreateOrUpdateChapterItemDTO validator){
        this.chapterService.updateChapterItem(id, validator);
        return new UpdatedVO();
    }

    // 上传课件
    @PostMapping("courseware")
    public CreatedVO createCourseware(@RequestBody CreateOrUpdateCoursewareDTO validator){
        this.courseService.uploadCourseware(validator);
        return new CreatedVO();
    }
}
